import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_module_sale/r.dart';
import '../bean/bean_tools_item.dart';

typedef ItemTapCallback = void Function(ToolsItem item);

class ToolsModuleWidget extends StatelessWidget {
  final Widget headChild; //容器内顶部视图，如：常用工具文案等
  final double leftSpacing; //容器距离父视图左间距，默认16
  final double rightSpacing; //容器距离父视图右间距，默认16
  final double bottomSpacing; //容器内部最后一排工具底部边距，默认30
  final double borderRadius; //圆角，默认0,
  final double mainAxisSpacing; //工具item垂直方向间距
  final int maxItems; //最多显示几个工具，默认不限制，-1
  final Color backgroundColor; //容器背景色
  final List<ToolsItem> data; //item数据
  final ItemTapCallback itemOnTap; //单个工具点击事件
  final Duration fadeOutDuration; //加载工具icon动画时间
  final Duration fadeInDuration;
  ToolsModuleWidget({
    this.headChild,
    this.leftSpacing = 16,
    this.rightSpacing = 16,
    this.mainAxisSpacing = 24,
    this.maxItems = -1,
    this.borderRadius = 0,
    this.itemOnTap,
    this.bottomSpacing = 28,
    this.backgroundColor = Colors.white,
    this.fadeOutDuration = const Duration(milliseconds: 1000),
    this.fadeInDuration = const Duration(milliseconds: 500),
    @required this.data,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 375,
      padding: EdgeInsets.only(left: leftSpacing, right: rightSpacing),
      color: backgroundColor,
      child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
            color: Colors.white,
          ),
          child: Column(
            children: <Widget>[
              _getHeadWidge(), //标题栏：如出房工具

              SizedBox(height: 12),
              listNotEmpty(data)
                  ? GridView.builder(
                      //工具item部分
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      padding: EdgeInsets.all(0),
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 4,
                        childAspectRatio: 1.2,
                        mainAxisSpacing: mainAxisSpacing,
                        crossAxisSpacing: 0,
                      ),
                      itemCount: maxItems > 0? ((data.length > maxItems) ?maxItems:data.length) :data.length,
                      itemBuilder: (context, index) {
                        ToolsItem item = data[index];
                        return _getToolItemWidget(item);
                      },
                    )
                  : SizedBox.shrink(),
              SizedBox(
                height: bottomSpacing,
              )
            ],
          )),
    );
  }

  Widget _getHeadWidge() {
    if (objNotEmpty(headChild)) {
      if (borderRadius > 0) {
        return Padding(
          padding:
              EdgeInsets.symmetric(horizontal: borderRadius), //防止头widget盖住圆角
          child: headChild,
        );
      }
      return headChild;
    }
    return SizedBox.shrink();
  }

  Widget _getToolItemWidget(ToolsItem item) {
    return InkWell(
      onTap: () {
        itemOnTap(item);
      },
      child: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ClipOval(
                child: CachedNetworkImage(
                    imageUrl: item.iconUrl ?? "",
                    fit: BoxFit.cover,
                    height: 43,
                    width: 43,
                    fadeOutDuration: fadeOutDuration,
                    fadeInDuration: fadeInDuration,
                    placeholder: (context, url) {
                      return Image.asset(R.imagesToolsToolIconPalceholderPng,
                          height: 43,
                          width: 43,
                          fit: BoxFit.cover);
                    },
                    errorWidget: (context, url,error) {
                      return Image.asset(R.imagesToolsToolIconPalceholderPng,
                          height: 43,
                          width: 43,
                          fit: BoxFit.cover);
                    }
                    )),
            Padding(
              padding: EdgeInsets.only(top: 7),
              child: DkText.simple(
                item.title ?? "",
                13,
                Color(0xff666666),
                fontWeight: FontWeight.normal,
              ),
            )
          ],
        ),
      ),
    );
  }
}

//工具module上面公用标题模块，比如出房工具
class ToolsCommonTitleWidget extends StatelessWidget {
  final String title;
  ToolsCommonTitleWidget(this.title);
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 2),
      height: 50,
      alignment: Alignment.centerLeft,
      child: DkText.simple(title ?? "", 18, Color(0xff333333)),
    );
  }
}
